package webUtils.buzhidao;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http 工具
 */
/**
 * @author 作者 linweiqiang
 * @version 创建时间：2017年9月29日 下午2:31:04
 * 类说明
 */
public class HttpTool {
    
    private static Logger logger = LoggerFactory.getLogger(HttpTool.class);
    
    private static String ENCODING = "UTF-8";
    
    private static String CONTENT_TYPE = "application/json";
    
    /**
     * 发送POST请求，参数是JSON
     * @throws UnsupportedEncodingException 
     */
    public static String requestPost(String url, List<NameValuePair> jsonParam) throws UnsupportedEncodingException{
        logger.debug("HttpTool.requestPost 开始 请求url：" + url + ", 参数：" + jsonParam);
//        System.out.println(jsonParam);
        //创建HttpClient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建HttpPost对象
        HttpPost httpPost = new HttpPost(url);
        //配置请求参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.DEFAULT)
                .setExpectContinueEnabled(true)
                .setSocketTimeout(5000)  
                .setConnectTimeout(5000)  
                .setConnectionRequestTimeout(5000)  
                .build();
        
        httpPost.setConfig(requestConfig);
        
        String respContent = null;
            
        //设置参数和请求方式
//        StringEntity entity = new StringEntity(jsonParam,ENCODING);//解决中文乱码问题    
//        entity.setContentEncoding(ENCODING);    
//        entity.setContentType(CONTENT_TYPE);  
        
        httpPost.setEntity(new UrlEncodedFormEntity(jsonParam,"utf-8"));
            
        HttpResponse resp;
        try {
            //执行请求
            resp = client.execute(httpPost);
            if(resp.getStatusLine().getStatusCode() == 200) {
                HttpEntity responseObj = resp.getEntity();
                respContent = EntityUtils.toString(responseObj,"UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("HttpTool.requestPost 异常 请求url：" + url + ", 参数：" + jsonParam + "，异常信息：" + e);
        }
        logger.error("HttpTool.requestPost 结束 请求url：" + url + ", 参数：" + jsonParam + "");
        //返回数据
        return respContent;
    }
}
